package com.google.code.p.localspinner;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.google.code.p.localspinner.http.UserAgent;

/**
 * Checks whether there's a new update
 * 
 * @author liangj01
 *
 */
class VersionChecker{
    private final UserAgent userAgent;
    private final String versionUrl;
    private final float currentVersion;
    private final Context ctx;
    private final NotificationManager notificationManager;

    public VersionChecker(Context ctx) {
        this.userAgent = new UserAgent();
        this.versionUrl = ctx.getResources().getString(R.string.config_version_url);
        this.currentVersion = Float.valueOf(ctx.getResources().getString(R.string.config_version));
        this.ctx = ctx;
        this.notificationManager = (NotificationManager)this.ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void checkVersionAvailable(){
        new Thread(new Runnable(){

            @Override
            public void run() {
                try {
                    BufferedReader br = new BufferedReader(new StringReader(userAgent.getUrlAsString(versionUrl)));
                    String line = null;
                    final List<String> versionInfo = new ArrayList<String>();
                    while((line = br.readLine())!= null){
                        versionInfo.add(line);
                    }
                    float version = Float.valueOf(versionInfo.get(0));
                    if(version > currentVersion){
                        notifyNewVersion(versionInfo);
                    }
                } catch (Exception e) {
                    Log.e(VersionChecker.class.toString(), "Failed to get versionInfo, skipping version check", e);
                }

            }
            
        }).start();
    }

    
    private void notifyNewVersion(List<String> versionInfo){
        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.notification_icon, versionInfo.get(2),
                System.currentTimeMillis());
        Intent downloadLink = new Intent(Intent.ACTION_VIEW, Uri.parse(versionInfo.get(1)));
        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, downloadLink, 0);
        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(ctx, versionInfo.get(3),
                versionInfo.get(4), contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        notificationManager.notify(R.layout.version_notification, notification);
    }

    
}